package com.ruoyi.web.controller.weiriya;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.weiriya.domain.WVehicleChargeType;
import com.ruoyi.weiriya.service.IWVehicleChargeTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 车辆充电类型Controller
 * 
 * @author lynn
 * @date 2023-08-01
 */
@Api(tags = "车辆充电类型管理")
@RestController
@RequestMapping("/weiriya/chargeType")
public class WVehicleChargeTypeController extends BaseController
{
    @Autowired
    private IWVehicleChargeTypeService wVehicleChargeTypeService;

    /**
     * 查询车辆充电类型列表
     */
    @ApiOperation("获取车辆充电类型列表")
    @PreAuthorize("@ss.hasPermi('weiriya:chargeType:list')")
    @GetMapping("/list")
    public TableDataInfo list(WVehicleChargeType wVehicleChargeType)
    {
        startPage();
        List<WVehicleChargeType> list = wVehicleChargeTypeService.list(wVehicleChargeType.queryWrapper());
        return getDataTable(list);
    }

    /**
     * 导出车辆充电类型列表
     */
    @ApiOperation("导出车辆充电类型列表")
    @PreAuthorize("@ss.hasPermi('weiriya:chargeType:export')")
    @Log(title = "车辆充电类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WVehicleChargeType wVehicleChargeType)
    {
        List<WVehicleChargeType> list = wVehicleChargeTypeService.list(wVehicleChargeType.queryWrapper());
        ExcelUtil<WVehicleChargeType> util = new ExcelUtil<WVehicleChargeType>(WVehicleChargeType.class);
        util.exportExcel(response, list, "车辆充电类型数据");
    }

    /**
     * 导入车辆充电类型列表
     */
    @ApiOperation("导入车辆充电类型列表")
    @PreAuthorize("@ss.hasPermi('weiriya:chargeType:import')")
    @Log(title = "车辆充电类型", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<WVehicleChargeType> util = new ExcelUtil<WVehicleChargeType>(WVehicleChargeType.class);
        List<WVehicleChargeType> list = util.importExcel(file.getInputStream());
        return success(wVehicleChargeTypeService.saveBatch(list));
    }

    /**
     * 获取车辆充电类型详细信息
     */
    @ApiOperation("获取车辆充电类型详细")
    @ApiImplicitParam(name = "id", value = "车辆充电类型ID", required = true, dataType = "String", paramType = "path")
    @PreAuthorize("@ss.hasPermi('weiriya:chargeType:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(wVehicleChargeTypeService.getById(id));
    }

    /**
     * 新增车辆充电类型
     */
    @ApiOperation("新增车辆充电类型")
    @PreAuthorize("@ss.hasPermi('weiriya:chargeType:add')")
    @Log(title = "车辆充电类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WVehicleChargeType wVehicleChargeType)
    {
        return toAjax(wVehicleChargeTypeService.saveType(wVehicleChargeType));
    }

    /**
     * 修改车辆充电类型
     */
    @ApiOperation("更新车辆充电类型")
    @PreAuthorize("@ss.hasPermi('weiriya:chargeType:edit')")
    @Log(title = "车辆充电类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WVehicleChargeType wVehicleChargeType)
    {
        WVehicleChargeType byId = wVehicleChargeTypeService.getById(wVehicleChargeType.getId());
        if(StringUtils.isNotNull(byId) && byId.getTypeValue().equals(wVehicleChargeType.getTypeValue())){
            return success();
        }
        List<WVehicleChargeType> list = wVehicleChargeTypeService.list(wVehicleChargeType.queryWrapper());
        if(list.size() > 0){
            throw new ServiceException("类型已存在！");
        }
        return toAjax(wVehicleChargeTypeService.updateById(wVehicleChargeType));
    }

    /**
     * 删除车辆充电类型
     */
    @ApiOperation("删除车辆充电类型")
    @ApiImplicitParam(name = "ids", value = "车辆充电类型ID集合", required = true, dataType = "String", paramType = "path")
    @PreAuthorize("@ss.hasPermi('weiriya:chargeType:remove')")
    @Log(title = "车辆充电类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<String> ids)
    {
        return toAjax(wVehicleChargeTypeService.removeTypeByIds(ids));
    }
}
